using System.BusinessObjects.With;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using Blazing.BusinessObjects.Cms;
using Blazing.BusinessObjects.Queries;
using Iesi.Collections.Generic;
using System.BusinessObjects.Data;

namespace Blazing.WebFacade
{
    [System.ComponentModel.DataObject]
    public class SiteController
    {
        public static int CurrentSiteId
        {
            get
            {
                if (HttpContext.Current.Session["siteId"] == null)
                    return -1;
                return (int)HttpContext.Current.Session["siteId"];
            }
            set
            {
                HttpContext.Current.Session["siteId"] = value;
            }
        }

        #region DataObject Methods
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void DeleteSite(Site site)
        {
            site.Delete();
            site.Save();
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int NewSite(Site site)
        {
            site.Save();
            return site.ID;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public Site UpdateSite(Site site)
        {
            Site.Evict(site.ID);
            Site update = Site.Load(site.ID);

             update.Name = site.Name;
             update.Url = site.Url;
             update.Save(SaveMode.Flush);

            FacadeCacheManager.Invalidate<Site>(site.Url);
            return update;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public IList<Site> GetSites()
        {
            return Site.Search(QrySearchSites.Query());
        }

        static object siteSyncObj = new object();
        public Site GetSite(string host)
        {
            Site retval = FacadeCacheManager.GetItem<Site, string>(arg0 => Site.Fetch(QrySearchSites.Query(arg0)),
                host, ref siteSyncObj, host);
            return retval;
        }

        public Site GetSiteOnly(string host)
        {
            Site retval = FacadeCacheManager.GetItem<Site, string>(arg0 => Site.Fetch(QrySearchSites.QuerySitesOnly(arg0)),
                host, ref siteSyncObj, host);
            return retval;
        }

        #endregion

        #region SiteAlias
        [DataObjectMethod(DataObjectMethodType.Select)]
        public ISet<SiteAlias> GetSiteAliases()
        {
            IList<Site> sites = Site.Search();
            ISet<SiteAlias> siteAliases = new HashedSet<SiteAlias>();

            foreach (Site site in sites)
            {
                siteAliases.AddAll(site.Aliases);
            }

            return siteAliases;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public SiteAlias DeleteSiteAlias(SiteAlias siteAlias)
        {
            With.Transaction.Execute(delegate
            {
                Site updatable = Site.Load(siteAlias.ParentSite.ID);
                foreach (SiteAlias p in updatable.Aliases)
                {
                    if (p.Name == siteAlias.Name && p.Url == siteAlias.Url)
                    {
                        updatable.Aliases.Remove(p);
                        break;
                    }
                }
                updatable.Save();
            });

            siteAlias.Delete();
            return siteAlias;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public SiteAlias NewSiteAlias(SiteAlias siteAlias)
        {
            Site updatable = Site.Load(siteAlias.ParentSite.ID); 
            With.Transaction.Execute(delegate
            {
                updatable.Aliases.Add(siteAlias);
                updatable.Save();
            });
            return siteAlias;
        }

        

        #endregion
    }
}
